<!-- eslint-disable no-undef -->
<script setup lang="ts">
import type { ConsultOrderItem } from '@/types/consult'
import { OrderType } from '@/enums'
import defaultImg from '@/assets/avatar-doctor.svg'
import { useShowPerviewPre, useCancelOrder, useDelOrder } from '@/composable'
const { showpreInfo } = useShowPerviewPre()
const { loading, cancelOrder } = useCancelOrder(() => {
  emits('cancelOrder', props.item.id)
})

const { delLoading, delOrder } = useDelOrder(() => {
  emits('delOrder', props.item.id)
})
const props = defineProps<{
  item: ConsultOrderItem
}>()
// const showPopover = ref(false)
// const actions = computed(() => [
//   { text: '查看处方', disabled: !props.item.prescriptionId },
//   { text: '删除订单' }
// ])
// const onSelect = (action: PopoverAction, i: number) => {
//   console.log('点击气泡项触发的方法', action, i)
//   // 点击了气泡中的删除按钮 删除订单
//   if (i === 1) {
//     delOrder(props.item)
//   }
//   // 查看处方
//   if (i === 0) {
//     showpreInfo(props.item.prescriptionId)
//   }
// }
const emits = defineEmits<{
  (e: 'cancelOrder', id: string): void
  (e: 'delOrder', id: string): void
}>()
/* const loading = ref(false)
// 取消问诊
const cancelOrder = async (item: ConsultOrderItem) => {
  try {
    loading.value = true
    // 1. 发送取消的请求
    await reqCancelOrder(item.id)
    loading.value = false
    showSuccessToast('取消成功')
    // 2. 子传父通知父组件更新视图
    emits('cancelOrder', item.id)
  } catch {
    loading.value = false
    showFailToast('取消失败')
  }
} */
/* const delLoading = ref(false)
// 删除订单
const delOrder = async (item: ConsultOrderItem) => {
  try {
    delLoading.value = true
    // 1. 发送取消的请求
    await reqDelOrder(item.id)
    delLoading.value = false
    showSuccessToast('删除成功')
    // 2. 子传父通知父组件更新视图
    emits('delOrder', item.id)
  } catch {
    delLoading.value = false
    showFailToast('删除失败')
  }
} */
</script>

<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" :src="item.docInfo?.avatar || defaultImg" />
      <p>{{ item.docInfo?.name }}</p>
      <span
        :class="{
          orange: item.status === OrderType.ConsultPay,
          green: item.status === OrderType.ConsultChat
        }"
        >{{ item.statusValue }}</span
      >
    </div>
    <div class="body" @click="$router.push(`/user/consult/${item.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ item.illnessDesc }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ item.payment }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ item.createTime }}</div>
      </div>
    </div>
    <!-- 待支付：取消问诊+去支付 -->
    <div class="foot" v-if="item.status === OrderType.ConsultPay">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="cancelOrder(item)"
        >取消问诊</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/user/consult/${item.id}`"
        >去支付</van-button
      >
    </div>
    <!-- 待接诊：取消问诊+继续沟通 -->
    <div class="foot" v-if="item.status === OrderType.ConsultWait">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="cancelOrder(item)"
        >取消问诊</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 咨询中：查看处方（如果开了）+继续沟通 -->
    <div class="foot" v-if="item.status === OrderType.ConsultChat">
      <van-button
        class="gray"
        plain
        size="small"
        v-if="item.prescriptionId"
        round
        @click="showpreInfo(item.prescriptionId)"
        >查看处方</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 已取消：删除订单+咨询其他医生 -->
    <div class="foot" v-if="item.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        round
        @click="delOrder(item)"
        :loading="delLoading"
        >删除订单</van-button
      >
      <van-button type="primary" plain size="small" round to="/"
        >咨询其他医生</van-button
      >
    </div>
    <!-- 已完成：更多（查看处方，如果开了，删除订单）+问诊记录+（未评价?写评价:查看评价）-->
    <div class="foot" v-if="item.status === OrderType.ConsultComplete">
      <!-- <div class="more">
        <van-popover
          v-model:show="showPopover"
          :actions="actions"
          @select="onSelect"
          placement="top-start"
        >
          <template #reference> 更多 </template>
        </van-popover>
      </div> -->
      <consult-more
        :item="item"
        @delOrder="delOrder"
        @showpreInfo="showpreInfo"
      ></consult-more>
      <van-button
        class="gray"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >问诊记录</van-button
      >
      <van-button
        class="gray"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        v-if="item.evaluateId"
        >查看评价</van-button
      >
      <van-button
        v-else
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >去评价</van-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
    .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    }
  }
}
</style>
